﻿using FineAdmin.IService;
using FineAdmin.Model;
using FineAdmin.Web.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FineAdmin.Web.Areas.Blog.Controllers
{
    public class FriendlySetController : BaseController
    {

        // GET: Blog/FriendlySet
        public IFriendlyService FriendlyService { get; set; }
        public override ActionResult Index(int? id)
        {
            base.Index(id);
            return View();
        }

        [HttpGet]
        public JsonResult List(FriendlyModel model, PageInfo pageInfo)
        {
            pageInfo.field = "Id";
            pageInfo.order = "desc";
            var result = FriendlyService.GetListByFilter(model, pageInfo);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Add()
        {

            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Add(FriendlyModel model)
        {

            model.CreateTime = DateTime.Now;
            model.CreateUserId = Operator.UserId;
            model.UpdateTime = DateTime.Now;
            model.UpdateUserId = Operator.UserId;
            var result = FriendlyService.Insert(model) ? SuccessTip("添加成功") : ErrorTip("添加失败");
            return Json(result);
        }

        public ActionResult Edit(int id)
        {

            var model = FriendlyService.GetById(id);

            return View(model);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(FriendlyModel model)
        {
            model.UpdateTime = DateTime.Now;
            model.UpdateUserId = Operator.UserId;
            var result = FriendlyService.UpdateById(model) ? SuccessTip("修改成功") : ErrorTip("修改失败");
            return Json(result);
        }

        [HttpGet]
        public JsonResult Delete(int id)
        {
            var result = FriendlyService.DeleteById(id) ? SuccessTip("删除成功") : ErrorTip("删除失败");
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult BatchDel(string idsStr)
        {
            var idsArray = idsStr.Substring(0, idsStr.Length - 1).Split(',');
            var result = FriendlyService.DeleteByIds(idsArray) ? SuccessTip("批量删除成功") : ErrorTip("批量删除失败");
            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }
}